#include "stdafx.h"
#include "jtable.h"
#include <assert.h>
JTable::JTable()
{

}

JTable::~JTable()
{
    clear();
}

JTablePtr JTable::NewTable()
{
    return JTablePtr(new JTable);
}

unsigned int JTable::AppendColumn(const std::wstring &colName)
{

    assert(this->m_keyMap.find(colName)==m_keyMap.end());
    unsigned int nn=m_keys.size();
    this->m_keys.push_back(colName);
    m_keyMap[colName]=nn;
    for(unsigned int i=0;i<m_tableData.size();++i)
    {
        m_tableData[i]->resize(nn);
    }
}

unsigned int JTable::InsertColumn(const std::wstring &colName, unsigned int pos)
{
    assert(this->m_keyMap.find(colName)==m_keyMap.end());
    if(pos>m_keys.size())
    {
        pos=this->m_keys.size();
    }
    m_keys.insert(m_keys.begin()+pos,colName);
    m_keyMap[colName]=pos;
    for(unsigned int i=0;i<m_tableData.size();++i)
    {
        auto ptr=m_tableData[i];
        ptr->insert(ptr->begin()+pos,wxVariant());
    }
}

unsigned int JTable::RemoveColumn(const std::wstring &colName)
{
    assert(this->m_keyMap.find(colName)!=m_keyMap.end());
    unsigned int idx=m_keyMap[colName];
    m_keyMap.erase(colName);
    m_keys.erase(m_keys.begin()+idx);
    for(unsigned int i=idx;i<m_keys.size();++i)
    {
        m_keyMap[m_keys[i]]=i;
    }
    for(unsigned int i=0;i<m_tableData.size();++i)
    {
        auto ptr=m_tableData[i];
        ptr->erase(ptr->begin()+i);
    }
}

unsigned int JTable::RemoveColumn(unsigned int colIdx)
{
    if(colIdx>m_keys.size())
    {
        colIdx=m_keys.size();
    }
    std::wstring colName=m_keys[colIdx];
    m_keyMap.erase(colName);
    m_keys.erase(m_keys.begin()+colIdx);
    for(unsigned int i=colIdx;i<m_keys.size();++i)
    {
        m_keyMap[m_keys[i]]=i;
    }
    for(unsigned int i=0;i<m_tableData.size();++i)
    {
        auto ptr=m_tableData[i];
        ptr->erase(ptr->begin()+i);
    }
}

void JTable::RemoveAllRows()
{
    for(unsigned int i=0;i<m_tableData.size();++i)
    {
        auto ptr=m_tableData[i];
        delete ptr;
    }
    m_tableData.clear();
}

void JTable::RemoveAllColumns()
{
    m_keys.clear();
    m_keyMap.clear();
    for(unsigned int i=0;i<m_tableData.size();++i)
    {
        auto ptr=m_tableData[i];
        ptr->clear();
    }
}

void JTable::clear()
{
    m_keys.clear();
    m_keyMap.clear();
    for(unsigned int i=0;i<m_tableData.size();++i)
    {
        auto ptr=m_tableData[i];
        delete ptr;
    }
    m_tableData.clear();
}


unsigned int JTable::SetRowCount(unsigned int newCount)
{
    unsigned int oldsz=this->m_tableData.size();
    if(oldsz<newCount)
    {
        this->m_tableData.resize(newCount);
        for(unsigned int i=oldsz;i<newCount;++i)
        {
            auto rd=new RowData;
            rd->resize(m_keys.size());
            m_tableData[i]=rd;
        }
    }else if(oldsz>newCount)
    {
        for(unsigned int i=newCount;i<oldsz;++i)
        {
            auto ptr=m_tableData[i];
            delete ptr;
        }
        m_tableData.resize(newCount);
    }
    return newCount;
}

unsigned int JTable::InsertRow(unsigned int pos)
{
    if(pos>m_tableData.size())
    {
        pos=this->m_tableData.size();
    }
    auto rd=new RowData;
    rd->resize(m_keys.size());
    this->m_tableData.insert(m_tableData.begin()+pos,rd);
    return pos;
}

unsigned int JTable::RemoveRow(unsigned int pos)
{
    if(m_tableData.size()==0)
        return 0;
    if(pos>=m_tableData.size())
    {
        pos=this->m_tableData.size()-1;
    }
    auto ptr=this->m_tableData[pos];
    delete ptr;
    m_tableData.erase(m_tableData.begin()+pos);
    return pos;
}

unsigned int JTable::AppendRow()
{
    unsigned int nr=this->m_tableData.size();
    auto rd=new RowData;
    rd->resize(m_keys.size());
    this->m_tableData.push_back(rd);
    return nr;
}



